<?php
namespace Products\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;

class HistoryTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function addHistory($buy_id,$type){
		$data = array(
			'id'        			=> $buy_id,	
			'date' 	 				=> date('Y-m-d H:i:s'),	
			'status' 	     		=> 0,
			'type' 	     			=> $type,			
			'updatenote' 	     	=> 'Pending Status',			
		);
		$this->tableGateway->insert($data);		
		return $this->tableGateway->lastInsertValue;
	}
	public function changeStatusbuyProduct($buy_id,$newStatus,$type,$updatenote){
		$select = $this->tableGateway->getSql()->select();
		$select->where('id="'.$buy_id.'"');	
		$select->where('status="'.$newStatus.'"');		
        $resultSet = $this->tableGateway->selectWith($select);
	    if($resultSet->count()==0){
			$data = array(
				'id'        			=> $buy_id,	
				'date' 	 				=> date('Y-m-d H:i:s'),
				'type'					=> $type,				
				'status' 	     		=> $newStatus,	
				'updatenote' 	       => $updatenote,	
			);
			$this->tableGateway->insert($data);		
			return $this->tableGateway->lastInsertValue;
		}else{
			return 0;
		}
	}
	public function deletebuyProduct($id){
	   $deleteid=$this->tableGateway->delete(array('(id IN ('.$id.'))'));
		return $deleteid;	
	}
}